﻿using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace LCL.Tools.WinFrm {
    public partial class StringTools:BaseFrm {
        int i = 0;
        public StringTools() {
            InitializeComponent();
        }
        private void txtContent_KeyDown(object sender,KeyEventArgs e) {
            if(e.KeyCode == Keys.F1) {
                if(txtContent.SelectedText != "")
                    Clipboard.SetDataObject(txtContent.SelectedText);
            } else if(e.KeyCode == Keys.F12) {
                Clipboard.SetDataObject(txtContent.SelectedText);
            } else if((!e.Alt && e.Control) && (e.KeyCode == Keys.C)) {
                Clipboard.SetDataObject(txtContent.SelectedText);
            }
        }     
        private void 拼接字符串ToolStripMenuItem_Click(object sender,EventArgs e) {
            try {
                #region 拼接字符串
                if(txtContent.Text.Trim()!="") {
                    StringBuilder l = new StringBuilder();
                    l.AppendLine("StringBuilder builder = new StringBuilder();");
                    string tempname = DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+
                        DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString();
                    string filename = System.Windows.Forms.Application.StartupPath+@"\\family_"+tempname+".txt";
                    StreamWriter sw2 = new StreamWriter(filename,false,Encoding.UTF8);
                    sw2.Write(txtContent.Text);
                    sw2.Flush();
                    sw2.Close();
                    sw2.Dispose();
                    txtContent.Text="";
                    System.IO.StreamReader read = new System.IO.StreamReader(filename);
                    while(true) {
                        string currentLine = read.ReadLine();
                        if(currentLine==null) {
                            break;
                        }
                        l.AppendLine("builder.AppendLine(\""+currentLine.Replace("\"","\\\"")+" \");");
                    }
                    read.Dispose();
                    File.Delete(filename);
                    txtContent.Text=l.ToString();
                }
                #endregion
            } catch(Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        private void 反拼接字符串ToolStripMenuItem_Click(object sender,EventArgs e) {
            try {
                #region 读取StringBuilder里的数据
                if(txtContent.Text.Trim()!="") {
                    StringBuilder lismsg = new StringBuilder();
                    string strContent = txtContent.Text;
                    string pattern = @"\(""(?<dd>.*?)""\)";
                    var reg = new Regex(pattern);
                    MatchCollection mc = reg.Matches(strContent);
                    foreach(Match m in mc) {
                        lismsg.AppendLine(m.Groups[1].Value);
                    }
                    txtContent.Text=lismsg.ToString();
                }
                #endregion
            } catch(Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        private void 大小写转换ToolStripMenuItem_Click(object sender,EventArgs e) {
            string temp = txtContent.Text;
            if(i==0) {
                txtContent.Text=temp.ToUpper();
                i=1;
            } else {
                txtContent.Text=temp.ToLower();
                i=0;
            }
        }

        private void cwitchCase转换ToolStripMenuItem_Click(object sender,EventArgs e) {
            try {
                #region 拼接字符串
                if(txtContent.Text.Trim()!="") {
                    StringBuilder l = new StringBuilder();
                    //l.AppendLine("StringBuilder builder = new StringBuilder();");
                    string tempname = DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+
                        DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString();

                    string filename = System.Windows.Forms.Application.StartupPath+@"\\family_"+tempname+".txt";
                    StreamWriter sw2 = new StreamWriter(filename,false,Encoding.UTF8);
                    sw2.Write(txtContent.Text);
                    sw2.Flush();
                    sw2.Close();
                    sw2.Dispose();
                    txtContent.Text="";
                    System.IO.StreamReader read = new System.IO.StreamReader(filename);
                    while(true) {
                        string currentLine = read.ReadLine();
                        if(currentLine==null) break;
                        string temp = currentLine.Replace("\"","\\\"").Replace("\r\n","").Replace("\t","").Trim();
                        l.AppendLine("case \""+temp+"\":");
                    }
                    read.Dispose();
                    File.Delete(filename);
                    txtContent.Text=l.ToString();
                    Clipboard.Clear();
                    Clipboard.SetText(l.ToString());
                }
                #endregion
            } catch(Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        private void 属性生成器ToolStripMenuItem_Click(object sender,EventArgs e) {
            try {
                #region 拼接字符串
                if(txtContent.Text.Trim()!="") {
                    StringBuilder l = new StringBuilder();
                    string tempname = DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+
                        DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString();

                    string filename = System.Windows.Forms.Application.StartupPath+@"\\family_"+tempname+".txt";
                    StreamWriter sw2 = new StreamWriter(filename,false,Encoding.UTF8);
                    sw2.Write(txtContent.Text);
                    sw2.Flush();
                    sw2.Close();
                    sw2.Dispose();
                    txtContent.Text="";
                    System.IO.StreamReader read = new System.IO.StreamReader(filename);

                    while(true) {
                        string currentLine = read.ReadLine();
                        if(currentLine==null) {
                            break;
                        }
                        l.AppendLine(" public string "+currentLine.Replace("\"","\\\"")+" ;");
                    }
                    read.Dispose();
                    File.Delete(filename);
                    txtContent.Text=l.ToString();
                }
                #endregion
            } catch(Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        private void 提取中文ToolStripMenuItem_Click(object sender,EventArgs e) {
            try {
                #region 提取中文
                StringBuilder l = new StringBuilder();
                string tempname = DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+
                    DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString();
                string filename = System.Windows.Forms.Application.StartupPath+@"\\family_"+tempname+".txt";
                StreamWriter sw2 = new StreamWriter(filename,false,Encoding.UTF8);
                sw2.Write(txtContent.Text);
                sw2.Flush();
                sw2.Close();
                sw2.Dispose();
                txtContent.Text="";
                System.IO.StreamReader read = new System.IO.StreamReader(filename);
                while(true) {
                    string currentLine = read.ReadLine();
                    if(currentLine==null) {
                        break;
                    }
                    string oriText = currentLine.Trim();
                    string x = @"[\u4E00-\u9FFF]+";
                    MatchCollection Matches = Regex.Matches(oriText,x,RegexOptions.IgnoreCase);
                    StringBuilder chineseString = new StringBuilder();
                    foreach(Match NextMatch in Matches) {
                        chineseString.Append(NextMatch.Value);
                    }
                    l.AppendLine(chineseString.ToString());
                }
                read.Dispose();
                File.Delete(filename);
                txtContent.Text=l.ToString();
                #endregion

            } catch(Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        private void liststringToolStripMenuItem_Click(object sender,EventArgs e) {
            try {
                #region 拼接成List<string>
                if(txtContent.Text.Trim()!="") {
                    StringBuilder l = new StringBuilder();
                    l.AppendLine("List<string> strList = new List<string>();");
                    string tempname = DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+
                        DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString();
                    string filename = System.Windows.Forms.Application.StartupPath+@"\\family_"+tempname+".txt";
                    StreamWriter sw2 = new StreamWriter(filename,false,Encoding.UTF8);
                    sw2.Write(txtContent.Text);
                    sw2.Flush();
                    sw2.Close();
                    sw2.Dispose();
                    txtContent.Text="";
                    System.IO.StreamReader read = new System.IO.StreamReader(filename);
                    while(true) {
                        string currentLine = read.ReadLine();
                        if(currentLine==null) {
                            break;
                        }
                        l.AppendLine("strList.Add(\""+currentLine.Replace("\"","\\\"")+" \");");
                    }
                    read.Dispose();
                    File.Delete(filename);
                    txtContent.Text=l.ToString();
                }
                #endregion
            } catch(Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        private void jS拼接字符串ToolStripMenuItem_Click(object sender,EventArgs e) {
            try {
                #region 拼接字符串
                if(txtContent.Text.Trim()!="") {
                    StringBuilder l = new StringBuilder();
                    string tempname = DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+
                        DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString();

                    string filename = System.Windows.Forms.Application.StartupPath+@"\\family_"+tempname+".txt";
                    StreamWriter sw2 = new StreamWriter(filename,false,Encoding.UTF8);
                    sw2.Write(txtContent.Text);
                    sw2.Flush();
                    sw2.Close();
                    sw2.Dispose();
                    txtContent.Text="";
                    System.IO.StreamReader read = new System.IO.StreamReader(filename);

                    while(true) {
                        string currentLine = read.ReadLine();
                        if(currentLine==null) {
                            break;
                        }
                        l.AppendLine("document.write(\""+currentLine.Replace("\"","'")+"\");");
                    }
                    read.Dispose();
                    File.Delete(filename);
                    txtContent.Text=l.ToString();
                }
                #endregion
            } catch {
                #region 拼接字符串
                if(txtContent.Text.Trim()!="") {
                    string temp = txtContent.Text
                        .Replace("\"","'")
                        .Replace(" ","")
                        .Replace("\n","")
                        .Replace("\r","")
                        .Replace("\r\n","")
                        .Trim();
                    txtContent.Text=temp;
                }
                #endregion
            }
        }

        private void 过滤HTMLToolStripMenuItem_Click(object sender,EventArgs e) {
            try {
                #region 过滤HTML
                string m_outstr = txtContent.Text.Clone() as string;
                m_outstr=new Regex(@"(?m)<script[^>]*>(\w|\W)*?</script[^>]*>",RegexOptions.Multiline|RegexOptions.IgnoreCase).Replace(m_outstr,string.Empty);
                m_outstr=new Regex(@"(?m)<style[^>]*>(\w|\W)*?</style[^>]*>",RegexOptions.Multiline|RegexOptions.IgnoreCase).Replace(m_outstr,string.Empty);
                m_outstr=new Regex(@"(?m)<link[^>]*>(\w|\W)*?[^>]*>",RegexOptions.Multiline|RegexOptions.IgnoreCase).Replace(m_outstr,string.Empty);
                m_outstr=new Regex(@"<iframe[\s\S]+</iframe *>",RegexOptions.Multiline|RegexOptions.IgnoreCase).Replace(m_outstr,string.Empty);
                m_outstr=new Regex(@"<frameset[\s\S]+</frameset  *>",RegexOptions.Multiline|RegexOptions.IgnoreCase).Replace(m_outstr,string.Empty);
                m_outstr=new Regex(@"-->",RegexOptions.Multiline|RegexOptions.IgnoreCase).Replace(m_outstr,string.Empty);
                m_outstr=new Regex(@"<!--.*",RegexOptions.Multiline|RegexOptions.IgnoreCase).Replace(m_outstr,string.Empty);
                m_outstr=new Regex(@"(&nbsp;)+").Replace(m_outstr,"");
                m_outstr=new Regex(@"(\r\n\r\n)+").Replace(m_outstr,"");
                txtContent.Text=m_outstr;
                #endregion

            } catch(Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        private void 空格转逗号拆分ToolStripMenuItem_Click(object sender,EventArgs e) {
            try {
                #region 过滤HTML
                string m_outstr = txtContent.Text.Clone() as string;

                m_outstr=new Regex(@"(\r\n\r\n)+").Replace(m_outstr,"、");
                m_outstr=m_outstr.Replace(" ","、"); 
                m_outstr=m_outstr.Replace("  ","、");
                m_outstr=m_outstr.Replace(@"\t","、");
                m_outstr= Regex.Replace(m_outstr,"\\s{1}","、");
                m_outstr= Regex.Replace(m_outstr,"\\s+","、");

                txtContent.Text=m_outstr;
                #endregion

            } catch(Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
